package cgl.narada.service.security.kmc.messages;

import java.io.IOException;
import java.io.Serializable;

import javax.crypto.SecretKey;

import org.apache.log4j.Logger;

import cgl.narada.service.security.kmc.SignedSecurityToken;
import cgl.narada.util.ObjectMarshaller;

/**
 * Contains the topic key encrypted with the entity's public key and the
 * security token <br>
 * Created on Sep 9, 2005
 * 
 * @author Harshawardhan Gadgil (hgadgil@grids.ucs.indiana.edu)
 */
public class SecureTopicKeyResponse implements Serializable {

    static Logger log = Logger.getLogger("SecureTopicKeyResponse");

    private SecretKey key;

    private SignedSecurityToken signedSecurityToken;

    /**
     * 
     */
    public SecureTopicKeyResponse(SecretKey _key,
            SignedSecurityToken _signedSecurityToken) {
        key = _key;
        signedSecurityToken = _signedSecurityToken;
    }

    /**
     * Marshalls the object into an array of bytes.
     * 
     * @return an array of bytes representing the marshaller object, null if
     *         error occurs
     */
    public byte[] getBytes() {
        try {
            return ObjectMarshaller.marshall(this);
        } catch (IOException e) {
            log.error("", e);
            return null;
        }
    }

    /**
     * Unmarshalls the bytes as a SecureTopicKeyResponse object
     * 
     * @param objectBytes
     * @return SecureTopicKeyResponse if successful, null if error occurs
     */
    public static SecureTopicKeyResponse createObjectFromBytes(byte[] objectBytes) {
        try {
            return (SecureTopicKeyResponse) ObjectMarshaller
                    .unmarshall(objectBytes);
        } catch (IOException e) {
            log.error("", e);
        } catch (ClassNotFoundException e) {
            log.error("", e);
        }
        return null;
    }

    /**
     * @return Returns the key.
     */
    public SecretKey getKey() {
        return key;
    }

    /**
     * @return Returns the signedSecurityToken.
     */
    public SignedSecurityToken getSignedSecurityToken() {
        return signedSecurityToken;
    }

}
